package com.qqt.csr.route.controller;

import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.idempotent.Idempotent;
import com.qqt.csr.common.interceptor.auth.Authorize;
import com.qqt.csr.common.log.Log;
import com.qqt.csr.common.operalog.BusinessType;
import com.qqt.csr.common.operalog.LogLevel;
import com.qqt.csr.common.operalog.OperationLog;
import com.qqt.csr.common.session.SessionContextHolder;
import com.qqt.csr.common.utils.ServiceAssert;
import com.qqt.csr.common.vo.req.PageRequest;
import com.qqt.csr.common.vo.resp.ObjectResponse;
import com.qqt.csr.common.vo.resp.PageResponse;
import com.qqt.csr.route.entity.RouteDistributionRule;
import com.qqt.csr.route.entity.ServiceTime;
import com.qqt.csr.route.service.RouteDistributionRuleService;
import com.qqt.csr.route.vo.req.*;
import com.qqt.csr.route.vo.resp.*;
import com.qqt.csr.workbench.convert.SkillSetConfigAssembler;
import com.qqt.csr.workbench.entity.SkillSetConfig;
import com.qqt.csr.workbench.vo.req.QueryingSkillSetConfigReqVO;
import com.qqt.csr.workbench.vo.resp.SkillSetConfigInfoRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Authorize
@RestController
@RequestMapping("route/manage")
@Tag(name = "路由管理", description = "路由管理")
public class RouteRulesController {
    @Autowired
    private RouteDistributionRuleService routeDistributionRuleService;

    @PostMapping("/add")
    @Operation(summary = "新增路由")
    @Log(desc = "新增路由")
    @Idempotent(model = "csr:route:add:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "路由管理", buttonName = "新增路由", businessType = BusinessType.INSERT)
    public ObjectResponse<String> add(@Validated @RequestBody AddingRouteDistributionRuleReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        Long creatorId = SessionContextHolder.getHeaderInfo().getUserId();
        Integer routeId = routeDistributionRuleService.add(tenantId, creatorId, req);
        return ObjectResponse.success(routeId.toString());
    }

    @PostMapping("/update")
    @Operation(summary = "修改路由")
    @Log(desc = "修改路由")
    @Idempotent(model = "csr:route:update:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "路由管理", buttonName = "修改路由", businessType = BusinessType.UPDATE)
    public ObjectResponse<String> update(@Validated @RequestBody UpdatingRouteDistributionRuleReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        routeDistributionRuleService.updateBy(tenantId, req);
        return ObjectResponse.success();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除路由")
    @Log(desc = "删除路由")
    @Idempotent(model = "csr:route:delete:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "路由管理", buttonName = "删除路由", businessType = BusinessType.DELETE)
    public ObjectResponse<String> delete(@Validated @RequestBody DeletingRouteRuleReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        routeDistributionRuleService.delete(tenantId, req);
        return ObjectResponse.success();
    }

    @PostMapping("/enable")
    @Operation(summary = "开启或关闭路由")
    @Log(desc = "开启或关闭路由")
    @Idempotent(model = "csr:route:enable:")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "路由管理", buttonName = "开启或关闭路由", businessType = BusinessType.UPDATE)
    public ObjectResponse<String> enable(@Validated @RequestBody UpdatingRouteRuleEnableReqVO req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        routeDistributionRuleService.enable(tenantId, req);
        return ObjectResponse.success();
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "路由详情")
    public ObjectResponse<RouteDistributionRuleDetailRespVO> detail(@PathVariable("id") Integer id) {
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        return ObjectResponse.success(routeDistributionRuleService.detail(tenantId, id));
    }

    @PostMapping("/queryPage")
    @Operation(summary = "分页查询路由")
    public PageResponse<RouteRulePageRespVO> queryPage(@Validated @RequestBody PageRequest<QueryingRouteRuleReqVO> req) {
        ServiceAssert.notNull(req, StatusCode.Common.NULL_ERROR.getCode(), "请求参数不能为空");
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        if(req.getData() == null){
            req.setData(new QueryingRouteRuleReqVO());
        }
        req.getData().setTenantId(tenantId);
        return routeDistributionRuleService.queryPage(req);
    }

    @GetMapping("/queryList")
    @Operation(summary = "查询路由下拉列表")
    public ObjectResponse<List<RouteRuleListRespVO>> queryList() {
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        List<RouteDistributionRule> rules = routeDistributionRuleService.queryList(tenantId);
        return ObjectResponse.success(rules.stream()
                .map(rule -> RouteRuleListRespVO.builder().id(rule.getId()).name(rule.getName()).build())
                .toList());
    }
}
